home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zchar1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  30.5 KB  |  1,045 lines

  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zchar1.c,v 1.5.2.1 2000/10/26 12:45:11 igorm Exp $ */
  20. /* Type 1 character display operator */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gsstruct.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gxdevice.h"        /* for gxfont.h */
  28. #include "gxfont.h"
  29. #include "gxfont1.h"
  30. #include "gxtype1.h"
  31. #include "gzstate.h"        /* for path for gs_type1_init */
  32.                 /* (should only be gsstate.h) */
  33. #include "gspaint.h"        /* for gs_fill, gs_stroke */
  34. #include "gspath.h"
  35. #include "gsrect.h"
  36. #include "estack.h"
  37. #include "ialloc.h"
  38. #include "ichar.h"
  39. #include "ichar1.h"
  40. #include "icharout.h"
  41. #include "idict.h"
  42. #include "ifont.h"
  43. #include "igstate.h"
  44. #include "iname.h"
  45. #include "store.h"
  46.  
  47. /* ---------------- Utilities ---------------- */
  48.  
  49. /* Test whether a font is a CharString font. */
  50. private bool
  51. font_uses_charstrings(const gs_font *pfont)
  52. {
  53.     return (pfont->FontType == ft_encrypted ||
  54.         pfont->FontType == ft_encrypted2 ||
  55.         pfont->FontType == ft_disk_based);
  56. }
  57.  
  58. /* Initialize a Type 1 interpreter. */
  59. private int
  60. type1_exec_init(gs_type1_state *pcis, gs_text_enum_t *penum,
  61.         gs_state *pgs, gs_font_type1 *pfont1)
  62. {
  63.     /*
  64.      * We have to disregard penum->pis and penum->path, and render to
  65.      * the current gstate and path.  This is a design bug that we will
  66.      * have to address someday!
  67.      */
  68.     return gs_type1_interp_init(pcis, (gs_imager_state *)pgs, pgs->path,
  69.                 &penum->log2_scale,
  70.                 (penum->text.operation & TEXT_DO_ANY_CHARPATH) != 0,
  71.                 pfont1->PaintType, pfont1);
  72. }
  73.  
  74. /* ---------------- .type1execchar ---------------- */
  75.  
  76. /*
  77.  * This is the workhorse for %Type1/2BuildChar, %Type1/2BuildGlyph,
  78.  * CCRun, and CID fonts.  Eventually this will appear in the C API;
  79.  * even now, its normal control path doesn't use any continuations.
  80.  */
  81.  
  82. /*
  83.  * Define the state record for this operator, which must save the metrics
  84.  * separately as well as the Type 1 interpreter state.
  85.  */
  86. typedef struct gs_type1exec_state_s {
  87.     gs_type1_state cis;        /* must be first */
  88.     i_ctx_t *i_ctx_p;        /* so push/pop can access o-stack */
  89.     double sbw[4];
  90.     int /*metrics_present */ present;
  91.     gs_rect char_bbox;
  92.     bool use_FontBBox_as_Metrics2;
  93.     /*
  94.      * The following elements are only used locally to make the stack clean
  95.      * for OtherSubrs: they don't need to be declared for the garbage
  96.      * collector.
  97.      */
  98.     ref save_args[6];
  99.     int num_args;
  100. } gs_type1exec_state;
  101.  
  102. gs_private_st_suffix_add1(st_gs_type1exec_state, gs_type1exec_state,
  103.               "gs_type1exec_state", gs_type1exec_state_enum_ptrs,
  104.               gs_type1exec_state_reloc_ptrs, st_gs_type1_state,
  105.               i_ctx_p);
  106.  
  107. /* Forward references */
  108. private int bbox_continue(P1(i_ctx_t *));
  109. private int nobbox_continue(P1(i_ctx_t *));
  110. private int type1_push_OtherSubr(P4(i_ctx_t *, const gs_type1exec_state *,
  111.                     int (*)(P1(i_ctx_t *)), const ref *));
  112. private int type1_call_OtherSubr(P4(i_ctx_t *, const gs_type1exec_state *,
  113.                     int (*)(P1(i_ctx_t *)), const ref *));
  114. private int type1_callout_dispatch(P3(i_ctx_t *, int (*)(P1(i_ctx_t *)),
  115.                       int));
  116. private int type1_continue_dispatch(P5(i_ctx_t *, gs_type1exec_state *,
  117.                        const ref *, ref *, int));
  118. private int op_type1_cleanup(P1(i_ctx_t *));
  119. private void op_type1_free(P1(i_ctx_t *));
  120. private void
  121.      type1_cis_get_metrics(P2(const gs_type1_state * pcis, double psbw[4]));
  122. private int bbox_getsbw_continue(P1(i_ctx_t *));
  123. private int type1exec_bbox(P3(i_ctx_t *, gs_type1exec_state *, gs_font *));
  124. private int bbox_finish_fill(P1(i_ctx_t *));
  125. private int bbox_finish_stroke(P1(i_ctx_t *));
  126. private int bbox_fill(P1(i_ctx_t *));
  127. private int bbox_stroke(P1(i_ctx_t *));
  128. private int nobbox_finish(P2(i_ctx_t *, gs_type1exec_state *));
  129. private int nobbox_draw(P2(i_ctx_t *, int (*)(P1(gs_state *))));
  130. private int nobbox_fill(P1(i_ctx_t *));
  131. private int nobbox_stroke(P1(i_ctx_t *));
  132.  
  133. /* <font> <code|name> <name> <charstring> .type1execchar - */
  134. private int
  135. ztype1execchar(i_ctx_t *i_ctx_p)
  136. {
  137.     return charstring_execchar(i_ctx_p, (1 << (int)ft_encrypted) |
  138.                    (1 << (int)ft_disk_based));
  139. }
  140. int
  141. charstring_execchar(i_ctx_t *i_ctx_p, int font_type_mask)
  142. {
  143.     os_ptr op = osp;
  144.     gs_font *pfont;
  145.     int code = font_param(op - 3, &pfont);
  146.     gs_font_base *const pbfont = (gs_font_base *) pfont;
  147.     gs_font_type1 *const pfont1 = (gs_font_type1 *) pfont;
  148.     const gs_type1_data *pdata;
  149.     gs_text_enum_t *penum = op_show_find(i_ctx_p);
  150.     gs_type1exec_state cxs;
  151.     gs_type1_state *const pcis = &cxs.cis;
  152.  
  153.     if (code < 0)
  154.     return code;
  155.     if (penum == 0 ||
  156.     pfont->FontType >= sizeof(font_type_mask) * 8 ||
  157.     !(font_type_mask & (1 << (int)pfont->FontType)))
  158.     return_error(e_undefined);
  159.     pdata = &pfont1->data;
  160.     /*
  161.      * Any reasonable implementation would execute something like
  162.      *    1 setmiterlimit 0 setlinejoin 0 setlinecap
  163.      * here, but the Adobe implementations don't.
  164.      *
  165.      * If this is a stroked font, set the stroke width.
  166.      */
  167.     if (pfont->PaintType)
  168.     gs_setlinewidth(igs, pfont->StrokeWidth);
  169.     check_estack(3);        /* for continuations */
  170.     /*
  171.      * Execute the definition of the character.
  172.      */
  173.     if (r_is_proc(op))
  174.     return zchar_exec_char_proc(i_ctx_p);
  175.     /*
  176.      * The definition must be a Type 1 CharString.
  177.      * Note that we do not require read access: this is deliberate.
  178.      */
  179.     check_type(*op, t_string);
  180.     if (r_size(op) <= max(pdata->lenIV, 0))
  181.     return_error(e_invalidfont);
  182.     /*
  183.      * In order to make character oversampling work, we must
  184.      * set up the cache before calling .type1addpath.
  185.      * To do this, we must get the bounding box from the FontBBox,
  186.      * and the width from the CharString or the Metrics.
  187.      * If the FontBBox isn't valid, we can't do any of this.
  188.      */
  189.     code = zchar_get_metrics(pbfont, op - 1, cxs.sbw);
  190.     if (code < 0)
  191.     return code;
  192.     cxs.present = code;
  193.     /* Establish a current point. */
  194.     code = gs_moveto(igs, 0.0, 0.0);
  195.     if (code < 0)
  196.     return code;
  197.     code = type1_exec_init(pcis, penum, igs, pfont1);
  198.     if (code < 0)
  199.     return code;
  200.     gs_type1_set_callback_data(pcis, &cxs);
  201.     if (pfont1->FontBBox.q.x > pfont1->FontBBox.p.x &&
  202.     pfont1->FontBBox.q.y > pfont1->FontBBox.p.y
  203.     ) {
  204.     /* The FontBBox appears to be valid. */
  205.     cxs.char_bbox = pfont1->FontBBox;
  206.     return type1exec_bbox(i_ctx_p, &cxs, pfont);
  207.     } else {
  208.     /*
  209.      * The FontBBox is not valid.  In this case,
  210.      * we create the path first, then do the setcachedevice.
  211.      * If we are oversampling (in this case, only for anti-
  212.      * aliasing, not just to improve quality), we have to
  213.      * create the path twice, since we can't know the
  214.      * oversampling factor until after setcachedevice.
  215.      */
  216.     const ref *opstr = op;
  217.     ref other_subr;
  218.  
  219.     if (cxs.present == metricsSideBearingAndWidth) {
  220.         gs_point sbpt;
  221.  
  222.         sbpt.x = cxs.sbw[0], sbpt.y = cxs.sbw[1];
  223.         gs_type1_set_lsb(pcis, &sbpt);
  224.     }
  225.     /* Continue interpreting. */
  226.       icont:
  227.     code = type1_continue_dispatch(i_ctx_p, &cxs, opstr, &other_subr, 4);
  228.     op = osp;        /* OtherSubrs might change it */
  229.     switch (code) {
  230.         case 0:        /* all done */
  231.         return nobbox_finish(i_ctx_p, &cxs);
  232.         default:        /* code < 0, error */
  233.         return code;
  234.         case type1_result_callothersubr:    /* unknown OtherSubr */
  235.         return type1_call_OtherSubr(i_ctx_p, &cxs, nobbox_continue,
  236.                         &other_subr);
  237.         case type1_result_sbw:    /* [h]sbw, just continue */
  238.         if (cxs.present != metricsSideBearingAndWidth) {
  239.             if ((penum->FontBBox_as_Metrics2.x == 0 &&
  240.              penum->FontBBox_as_Metrics2.y == 0) ||
  241.                 gs_rootfont(igs)->WMode == 0
  242.             ) {
  243.                  type1_cis_get_metrics(pcis, cxs.sbw);
  244.                 cxs.use_FontBBox_as_Metrics2 = false;
  245.                 } else { /* pass here if FontType==9,11 && WMode==1*/
  246.                 cxs.sbw[0] = penum->FontBBox_as_Metrics2.x / 2;
  247.                 cxs.sbw[1] = penum->FontBBox_as_Metrics2.y;
  248.                 cxs.sbw[2] = 0;
  249.                 cxs.sbw[3] = -penum->FontBBox_as_Metrics2.x; /* Sic! */
  250.                 cxs.use_FontBBox_as_Metrics2 = true;
  251.             cxs.present = metricsSideBearingAndWidth;
  252.             }
  253.         }
  254.         opstr = 0;
  255.         goto icont;
  256.     }
  257.     }
  258. }
  259.  
  260. /* -------- bbox case -------- */
  261.  
  262. /* Do all the work for the case where we have a bounding box. */
  263. private int
  264. type1exec_bbox(i_ctx_t *i_ctx_p, gs_type1exec_state * pcxs,
  265.            gs_font * pfont)
  266. {
  267.     os_ptr op = osp;
  268.     gs_type1_state *const pcis = &pcxs->cis;
  269.     gs_font_base *const pbfont = (gs_font_base *) pfont;
  270.  
  271.     /*
  272.      * We appear to have a valid bounding box.  If we don't have Metrics for
  273.      * this character, start interpreting the CharString; do the
  274.      * setcachedevice as soon as we know the (side bearing and) width.
  275.      */
  276.     if (pcxs->present == metricsNone) {
  277.     /* Get the width from the CharString, */
  278.     /* then set the cache device. */
  279.     ref cnref;
  280.     ref other_subr;
  281.     int code;
  282.  
  283.     /* Since an OtherSubr callout might change osp, */
  284.     /* save the character name now. */
  285.     ref_assign(&cnref, op - 1);
  286.     code = type1_continue_dispatch(i_ctx_p, pcxs, op, &other_subr, 4);
  287.     op = osp;        /* OtherSubrs might change it */
  288.     switch (code) {
  289.         default:        /* code < 0 or done, error */
  290.         return ((code < 0 ? code :
  291.              gs_note_error(e_invalidfont)));
  292.         case type1_result_callothersubr:    /* unknown OtherSubr */
  293.         return type1_call_OtherSubr(i_ctx_p, pcxs,
  294.                         bbox_getsbw_continue,
  295.                         &other_subr);
  296.         case type1_result_sbw:    /* [h]sbw, done */
  297.         break;
  298.     }
  299.     type1_cis_get_metrics(pcis, pcxs->sbw);
  300.     return zchar_set_cache(i_ctx_p, pbfont, &cnref,
  301.                    NULL, pcxs->sbw + 2,
  302.                    &pcxs->char_bbox,
  303.                    bbox_finish_fill, bbox_finish_stroke, NULL);
  304.     } else {
  305.     /* We have the width and bounding box: */
  306.     /* set up the cache device now. */
  307.     return zchar_set_cache(i_ctx_p, pbfont, op - 1,
  308.                    (pcxs->present ==
  309.                 metricsSideBearingAndWidth ?
  310.                 pcxs->sbw : NULL),
  311.                    pcxs->sbw + 2,
  312.                    &pcxs->char_bbox,
  313.                    bbox_finish_fill, bbox_finish_stroke, NULL);
  314.     }
  315. }
  316.  
  317. /* Continue from an OtherSubr callout while getting metrics. */
  318. private int
  319. bbox_getsbw_continue(i_ctx_t *i_ctx_p)
  320. {
  321.     os_ptr op = osp;
  322.     ref other_subr;
  323.     gs_type1exec_state *pcxs = r_ptr(esp, gs_type1exec_state);
  324.     gs_type1_state *const pcis = &pcxs->cis;
  325.     int code;
  326.  
  327.     code = type1_continue_dispatch(i_ctx_p, pcxs, NULL, &other_subr, 4);
  328.     op = osp;            /* in case z1_push/pop_proc was called */
  329.     switch (code) {
  330.     default:        /* code < 0 or done, error */
  331.         op_type1_free(i_ctx_p);
  332.         return ((code < 0 ? code : gs_note_error(e_invalidfont)));
  333.     case type1_result_callothersubr:    /* unknown OtherSubr */
  334.         return type1_push_OtherSubr(i_ctx_p, pcxs, bbox_getsbw_continue,
  335.                     &other_subr);
  336.     case type1_result_sbw: {    /* [h]sbw, done */
  337.         double sbw[4];
  338.         const gs_font_base *const pbfont =
  339.         (const gs_font_base *)pcis->pfont;
  340.         gs_rect bbox;
  341.  
  342.         /* Get the metrics before freeing the state. */
  343.         type1_cis_get_metrics(pcis, sbw);
  344.         bbox = pcxs->char_bbox;
  345.         op_type1_free(i_ctx_p);
  346.         return zchar_set_cache(i_ctx_p, pbfont, op, sbw, sbw + 2, &bbox,
  347.                    bbox_finish_fill, bbox_finish_stroke, NULL);
  348.     }
  349.     }
  350. }
  351.  
  352. /* <font> <code|name> <name> <charstring> <sbx> <sby> %bbox_{fill|stroke} - */
  353. /* <font> <code|name> <name> <charstring> %bbox_{fill|stroke} - */
  354. private int bbox_finish(P2(i_ctx_t *, int (*)(P1(i_ctx_t *))));
  355. private int
  356. bbox_finish_fill(i_ctx_t *i_ctx_p)
  357. {
  358.     return bbox_finish(i_ctx_p, bbox_fill);
  359. }
  360. private int
  361. bbox_finish_stroke(i_ctx_t *i_ctx_p)
  362. {
  363.     return bbox_finish(i_ctx_p, bbox_stroke);
  364. }
  365. private int
  366. bbox_finish(i_ctx_t *i_ctx_p, int (*cont) (P1(i_ctx_t *)))
  367. {
  368.     os_ptr op = osp;
  369.     gs_font *pfont;
  370.     int code;
  371.     gs_text_enum_t *penum = op_show_find(i_ctx_p);
  372.     gs_type1exec_state cxs;    /* stack allocate to avoid sandbars */
  373.     gs_type1_state *const pcis = &cxs.cis;
  374.     double sbxy[2];
  375.     gs_point sbpt;
  376.     gs_point *psbpt = 0;
  377.     os_ptr opc = op;
  378.     const ref *opstr;
  379.     ref other_subr;
  380.  
  381.     if (!r_has_type(opc, t_string)) {
  382.     check_op(3);
  383.     code = num_params(op, 2, sbxy);
  384.     if (code < 0)
  385.         return code;
  386.     sbpt.x = sbxy[0];
  387.     sbpt.y = sbxy[1];
  388.     psbpt = &sbpt;
  389.     opc -= 2;
  390.     check_type(*opc, t_string);
  391.     }
  392.     code = font_param(opc - 3, &pfont);
  393.     if (code < 0)
  394.     return code;
  395.     if (penum == 0 || !font_uses_charstrings(pfont))
  396.     return_error(e_undefined);
  397.     {
  398.     gs_font_type1 *const pfont1 = (gs_font_type1 *) pfont;
  399.     int lenIV = pfont1->data.lenIV;
  400.  
  401.     if (lenIV > 0 && r_size(opc) <= lenIV)
  402.         return_error(e_invalidfont);
  403.     check_estack(5);    /* in case we need to do a callout */
  404.     code = type1_exec_init(pcis, penum, igs, pfont1);
  405.     if (code < 0)
  406.         return code;
  407.     if (psbpt)
  408.         gs_type1_set_lsb(pcis, psbpt);
  409.     }
  410.     opstr = opc;
  411.   icont:
  412.     code = type1_continue_dispatch(i_ctx_p, &cxs, opstr, &other_subr,
  413.                    (psbpt ? 6 : 4));
  414.     op = osp;        /* OtherSubrs might have altered it */
  415.     switch (code) {
  416.     case 0:        /* all done */
  417.         /* Call the continuation now. */
  418.         if (psbpt)
  419.         pop(2);
  420.         return (*cont)(i_ctx_p);
  421.     case type1_result_callothersubr:    /* unknown OtherSubr */
  422.         push_op_estack(cont);    /* call later */
  423.         return type1_call_OtherSubr(i_ctx_p, &cxs, bbox_continue,
  424.                     &other_subr);
  425.     case type1_result_sbw:    /* [h]sbw, just continue */
  426.         opstr = 0;
  427.         goto icont;
  428.     default:        /* code < 0, error */
  429.         return code;
  430.     }
  431. }
  432.  
  433. private int
  434. bbox_continue(i_ctx_t *i_ctx_p)
  435. {
  436.     os_ptr op = osp;
  437.     int npop = (r_has_type(op, t_string) ? 4 : 6);
  438.     int code = type1_callout_dispatch(i_ctx_p, bbox_continue, npop);
  439.  
  440.     if (code == 0) {
  441.     op = osp;        /* OtherSubrs might have altered it */
  442.     npop -= 4;        /* nobbox_fill/stroke handles the rest */
  443.     pop(npop);
  444.     op -= npop;
  445.     op_type1_free(i_ctx_p);
  446.     }
  447.     return code;
  448. }
  449.  
  450. /*
  451.  * Check the path against FontBBox before drawing.  The original operands
  452.  * of type1execchar are still on the o-stack.
  453.  */
  454. private int
  455. bbox_draw(i_ctx_t *i_ctx_p, int (*draw)(P1(gs_state *)))
  456. {
  457.     os_ptr op = osp;
  458.     gs_rect bbox;
  459.     gs_font *pfont;
  460.     gs_text_enum_t *penum;
  461.     gs_font_base * pbfont;
  462.     gs_font_type1 * pfont1;
  463.     gs_type1exec_state cxs;
  464.     int code;
  465.  
  466.     if (igs->in_cachedevice < 2)    /* not caching */
  467.     return nobbox_draw(i_ctx_p, draw);
  468.     if ((code = font_param(op - 3, &pfont)) < 0)
  469.     return code;
  470.     penum = op_show_find(i_ctx_p);
  471.     if (penum == 0 || !font_uses_charstrings(pfont))
  472.     return_error(e_undefined);
  473.     if ((code = gs_pathbbox(igs, &bbox)) < 0) {
  474.     /*
  475.      * If the matrix is singular, all user coordinates map onto a
  476.      * straight line.  Don't bother rendering the character at all.
  477.      */
  478.     if (code == e_undefinedresult) {
  479.         pop(4);
  480.         gs_newpath(igs);
  481.         return 0;
  482.     }
  483.     return code;
  484.     }
  485.     if (draw == gs_stroke) {
  486.     /* Expand the bounding box by the line width. */
  487.     float width = gs_currentlinewidth(igs) * 1.41422;
  488.  
  489.     bbox.p.x -= width, bbox.p.y -= width;
  490.     bbox.q.x += width, bbox.q.y += width;
  491.     }
  492.     pbfont = (gs_font_base *)pfont;
  493.     if (rect_within(bbox, pbfont->FontBBox))    /* within bounds */
  494.     return nobbox_draw(i_ctx_p, draw);
  495.     /* Enlarge the FontBBox to save work in the future. */
  496.     rect_merge(pbfont->FontBBox, bbox);
  497.     /* Dismantle everything we've done, and start over. */
  498.     gs_text_retry(penum);
  499.     pfont1 = (gs_font_type1 *) pfont;
  500.     code = zchar_get_metrics(pbfont, op - 1, cxs.sbw);
  501.     if (code < 0)
  502.     return code;
  503.     cxs.present = code;
  504.     code = type1_exec_init(&cxs.cis, penum, igs, pfont1);
  505.     if (code < 0)
  506.     return code;
  507.     cxs.char_bbox = pfont1->FontBBox;
  508.     return type1exec_bbox(i_ctx_p, &cxs, pfont);
  509. }
  510. private int
  511. bbox_fill(i_ctx_t *i_ctx_p)
  512. {
  513.     /* See nobbox_fill for why we use eofill here. */
  514.     return bbox_draw(i_ctx_p, gs_eofill);
  515. }
  516. private int
  517. bbox_stroke(i_ctx_t *i_ctx_p)
  518. {
  519.     return bbox_draw(i_ctx_p, gs_stroke);
  520. }
  521.  
  522. /* -------- Common code -------- */
  523.  
  524. /* Get the metrics (l.s.b. and width) from the Type 1 interpreter. */
  525. private void
  526. type1_cis_get_metrics(const gs_type1_state * pcis, double psbw[4])
  527. {
  528.     psbw[0] = fixed2float(pcis->lsb.x);
  529.     psbw[1] = fixed2float(pcis->lsb.y);
  530.     psbw[2] = fixed2float(pcis->width.x);
  531.     psbw[3] = fixed2float(pcis->width.y);
  532. }
  533.  
  534. /* Handle the results of interpreting the CharString. */
  535. /* pcref points to a t_string ref. */
  536. private int
  537. type1_continue_dispatch(i_ctx_t *i_ctx_p, gs_type1exec_state *pcxs,
  538.             const ref * pcref, ref *pos, int num_args)
  539. {
  540.     int value;
  541.     int code;
  542.     gs_const_string charstring;
  543.     gs_const_string *pchars;
  544.  
  545.     if (pcref == 0) {
  546.     pchars = 0;
  547.     } else {
  548.     charstring.data = pcref->value.const_bytes;
  549.     charstring.size = r_size(pcref);
  550.     pchars = &charstring;
  551.     }
  552.     /*
  553.      * Since OtherSubrs may push or pop values on the PostScript operand
  554.      * stack, remove the arguments of .type1execchar before calling the
  555.      * Type 1 interpreter, and put them back afterwards unless we're
  556.      * about to execute an OtherSubr procedure.  Also, we must set up
  557.      * the callback data for pushing OtherSubrs arguments.
  558.      */
  559.     pcxs->i_ctx_p = i_ctx_p;
  560.     pcxs->num_args = num_args;
  561.     memcpy(pcxs->save_args, osp - (num_args - 1), num_args * sizeof(ref));
  562.     osp -= num_args;
  563.     gs_type1_set_callback_data(&pcxs->cis, pcxs);
  564.     code = pcxs->cis.pfont->data.interpret(&pcxs->cis, pchars, &value);
  565.     switch (code) {
  566.     case type1_result_callothersubr: {
  567.         /*
  568.          * The Type 1 interpreter handles all known OtherSubrs,
  569.          * so this must be an unknown one.
  570.          */
  571.         const font_data *pfdata = pfont_data(gs_currentfont(igs));
  572.  
  573.         code = array_get(&pfdata->u.type1.OtherSubrs, (long)value, pos);
  574.         if (code >= 0)
  575.         return type1_result_callothersubr;
  576.     }
  577.     }
  578.     /* Put back the arguments removed above. */
  579.     memcpy(osp + 1, pcxs->save_args, num_args * sizeof(ref));
  580.     osp += num_args;
  581.     return code;
  582. }
  583.  
  584. /*
  585.  * Push a continuation, the arguments removed for the OtherSubr, and
  586.  * the OtherSubr procedure.
  587.  */
  588. private int
  589. type1_push_OtherSubr(i_ctx_t *i_ctx_p, const gs_type1exec_state *pcxs,
  590.              int (*cont)(P1(i_ctx_t *)), const ref *pos)
  591. {
  592.     int i, n = pcxs->num_args;
  593.  
  594.     push_op_estack(cont);
  595.     /*
  596.      * Push the saved arguments (in reverse order, so they will get put
  597.      * back on the operand stack in the correct order) on the e-stack.
  598.      */
  599.     for (i = n; --i >= 0; ) {
  600.     *++esp = pcxs->save_args[i];
  601.     r_clear_attrs(esp, a_executable);  /* just in case */
  602.     }
  603.     ++esp;
  604.     *esp = *pos;
  605.     return o_push_estack;
  606. }
  607.  
  608. /*
  609.  * Do a callout to an OtherSubr implemented in PostScript.
  610.  * The caller must have done a check_estack(4 + num_args).
  611.  */
  612. private int
  613. type1_call_OtherSubr(i_ctx_t *i_ctx_p, const gs_type1exec_state * pcxs,
  614.              int (*cont) (P1(i_ctx_t *)),
  615.              const ref * pos)
  616. {
  617.     /* Move the Type 1 interpreter state to the heap. */
  618.     gs_type1exec_state *hpcxs =
  619.     ialloc_struct(gs_type1exec_state, &st_gs_type1exec_state,
  620.               "type1_call_OtherSubr");
  621.  
  622.     if (hpcxs == 0)
  623.     return_error(e_VMerror);
  624.     *hpcxs = *pcxs;
  625.     gs_type1_set_callback_data(&hpcxs->cis, hpcxs);
  626.     push_mark_estack(es_show, op_type1_cleanup);
  627.     ++esp;
  628.     make_istruct(esp, 0, hpcxs);
  629.     return type1_push_OtherSubr(i_ctx_p, pcxs, cont, pos);
  630. }
  631.  
  632. /* Continue from an OtherSubr callout while building the path. */
  633. private int
  634. type1_callout_dispatch(i_ctx_t *i_ctx_p, int (*cont)(P1(i_ctx_t *)),
  635.                int num_args)
  636. {
  637.     ref other_subr;
  638.     gs_type1exec_state *pcxs = r_ptr(esp, gs_type1exec_state);
  639.     int code;
  640.  
  641.   icont:
  642.     code = type1_continue_dispatch(i_ctx_p, pcxs, NULL, &other_subr,
  643.                    num_args);
  644.     switch (code) {
  645.     case 0:        /* callout done, cont is on e-stack */
  646.         return 0;
  647.     default:        /* code < 0 or done, error */
  648.         op_type1_free(i_ctx_p);
  649.         return ((code < 0 ? code : gs_note_error(e_invalidfont)));
  650.     case type1_result_callothersubr:    /* unknown OtherSubr */
  651.         return type1_push_OtherSubr(i_ctx_p, pcxs, cont, &other_subr);
  652.     case type1_result_sbw:    /* [h]sbw, just continue */
  653.         goto icont;
  654.     }
  655. }
  656.  
  657. /* Clean up after a Type 1 callout. */
  658. private int
  659. op_type1_cleanup(i_ctx_t *i_ctx_p)
  660. {
  661.     ifree_object(r_ptr(esp + 2, void), "op_type1_cleanup");
  662.     return 0;
  663. }
  664. private void
  665. op_type1_free(i_ctx_t *i_ctx_p)
  666. {
  667.     ifree_object(r_ptr(esp, void), "op_type1_free");
  668.     /*
  669.      * In order to avoid popping from the e-stack and then pushing onto
  670.      * it, which would violate an interpreter invariant, we simply
  671.      * overwrite the two e-stack items being discarded (hpcxs and the
  672.      * cleanup operator) with empty procedures.
  673.      */
  674.     make_empty_const_array(esp - 1, a_readonly + a_executable);
  675.     make_empty_const_array(esp, a_readonly + a_executable);
  676. }
  677.  
  678. /* -------- no-bbox case -------- */
  679.  
  680. private int
  681. nobbox_continue(i_ctx_t *i_ctx_p)
  682. {
  683.     int code = type1_callout_dispatch(i_ctx_p, nobbox_continue, 4);
  684.  
  685.     if (code)
  686.     return code;
  687.     {
  688.     gs_type1exec_state *pcxs = r_ptr(esp, gs_type1exec_state);
  689.     gs_type1exec_state cxs;
  690.  
  691.     cxs = *pcxs;
  692.     gs_type1_set_callback_data(&cxs.cis, &cxs);
  693.     op_type1_free(i_ctx_p);
  694.     return nobbox_finish(i_ctx_p, &cxs);
  695.     }
  696. }
  697.  
  698. /* Finish the no-FontBBox case after constructing the path. */
  699. /* If we are oversampling for anti-aliasing, we have to go around again. */
  700. /* <font> <code|name> <name> <charstring> %nobbox_continue - */
  701. private int
  702. nobbox_finish(i_ctx_t *i_ctx_p, gs_type1exec_state * pcxs)
  703. {
  704.     os_ptr op = osp;
  705.     int code;
  706.     gs_text_enum_t *penum = op_show_find(i_ctx_p);
  707.     gs_font *pfont;
  708.  
  709.     if ((code = gs_pathbbox(igs, &pcxs->char_bbox)) < 0 ||
  710.     (code = font_param(op - 3, &pfont)) < 0
  711.     )
  712.     return code;
  713.     if (penum == 0 || !font_uses_charstrings(pfont))
  714.     return_error(e_undefined);
  715.     {
  716.     gs_font_base *const pbfont = (gs_font_base *) pfont;
  717.     gs_font_type1 *const pfont1 = (gs_font_type1 *) pfont;
  718.  
  719.     if (pcxs->present == metricsNone) {
  720.         gs_point endpt;
  721.  
  722.         if ((code = gs_currentpoint(igs, &endpt)) < 0)
  723.         return code;
  724.         pcxs->sbw[2] = endpt.x, pcxs->sbw[3] = endpt.y;
  725.         pcxs->present = metricsSideBearingAndWidth;
  726.     }
  727.     /*
  728.      * We only need to rebuild the path from scratch if we might
  729.      * oversample for anti-aliasing.
  730.      */
  731.     if ((*dev_proc(igs->device, get_alpha_bits))(igs->device, go_text) > 1
  732.         ) {
  733.         gs_newpath(igs);
  734.         gs_moveto(igs, 0.0, 0.0);
  735.         code = type1_exec_init(&pcxs->cis, penum, igs, pfont1);
  736.         if (code < 0)
  737.         return code;
  738.         return type1exec_bbox(i_ctx_p, pcxs, pfont);
  739.     }
  740.     return zchar_set_cache(i_ctx_p, pbfont, op, NULL,
  741.                            pcxs->sbw + 2,
  742.                    &pcxs->char_bbox,
  743.                    nobbox_fill, nobbox_stroke,
  744.                    (pcxs->use_FontBBox_as_Metrics2 ? pcxs->sbw : NULL));
  745.     }
  746. }
  747. /* Finish by popping the operands and filling or stroking. */
  748. private int
  749. nobbox_draw(i_ctx_t *i_ctx_p, int (*draw)(P1(gs_state *)))
  750. {
  751.     int code = draw(igs);
  752.  
  753.     if (code >= 0)
  754.     pop(4);
  755.     return code;
  756. }
  757. private int
  758. nobbox_fill(i_ctx_t *i_ctx_p)
  759. {
  760.     /*
  761.      * Properly designed fonts, which have no self-intersecting outlines
  762.      * and in which outer and inner outlines are drawn in opposite
  763.      * directions, aren't affected by choice of filling rule; but some
  764.      * badly designed fonts in the Genoa test suite seem to require
  765.      * using the even-odd rule to match Adobe interpreters.
  766.      */
  767.     return nobbox_draw(i_ctx_p, gs_eofill);
  768. }
  769. private int
  770. nobbox_stroke(i_ctx_t *i_ctx_p)
  771. {
  772.     return nobbox_draw(i_ctx_p, gs_stroke);
  773. }
  774.  
  775. /* ------ Initialization procedure ------ */
  776.  
  777. const op_def zchar1_op_defs[] =
  778. {
  779.     {"4.type1execchar", ztype1execchar},
  780.         /* Internal operators */
  781.     {"4%bbox_getsbw_continue", bbox_getsbw_continue},
  782.     {"4%bbox_continue", bbox_continue},
  783.     {"4%bbox_finish_fill", bbox_finish_fill},
  784.     {"4%bbox_finish_stroke", bbox_finish_stroke},
  785.     {"4%nobbox_continue", nobbox_continue},
  786.     {"4%nobbox_fill", nobbox_fill},
  787.     {"4%nobbox_stroke", nobbox_stroke},
  788.     op_def_end(0)
  789. };
  790.  
  791. /* ------ Auxiliary procedures for type 1 fonts ------ */
  792.  
  793. private int
  794. z1_glyph_data(gs_font_type1 * pfont, gs_glyph glyph, gs_const_string * pstr)
  795. {
  796.     ref gref;
  797.  
  798.     glyph_ref(glyph, &gref);
  799.     return zchar_charstring_data((gs_font *)pfont, &gref, pstr);
  800. }
  801.  
  802. private int
  803. z1_subr_data(gs_font_type1 * pfont, int index, bool global,
  804.          gs_const_string * pstr)
  805. {
  806.     const font_data *pfdata = pfont_data(pfont);
  807.     ref subr;
  808.     int code;
  809.  
  810.     code = array_get((global ? &pfdata->u.type1.GlobalSubrs :
  811.               &pfdata->u.type1.Subrs),
  812.              index, &subr);
  813.     if (code < 0)
  814.     return code;
  815.     check_type_only(subr, t_string);
  816.     pstr->data = subr.value.const_bytes;
  817.     pstr->size = r_size(&subr);
  818.     return 0;
  819. }
  820.  
  821. private int
  822. z1_seac_data(gs_font_type1 *pfont, int ccode, gs_glyph *pglyph,
  823.          gs_const_string *pstr)
  824. {
  825.     ref std_glyph;
  826.     int code = array_get(&StandardEncoding, (long)ccode, &std_glyph);
  827.  
  828.     if (code < 0)
  829.     return code;
  830.     if (pglyph) {
  831.     switch (r_type(&std_glyph)) {
  832.     case t_name:
  833.         *pglyph = name_index(&std_glyph);
  834.         break;
  835.     case t_integer:
  836.         *pglyph = gs_min_cid_glyph + std_glyph.value.intval;
  837.         if (*pglyph < gs_min_cid_glyph || *pglyph > gs_max_glyph)
  838.         *pglyph = gs_no_glyph;
  839.         break;
  840.     default:
  841.         return_error(e_typecheck);
  842.     }
  843.     }
  844.     if (pstr)
  845.     code = zchar_charstring_data((gs_font *)pfont, &std_glyph, pstr);
  846.     return code;
  847. }
  848.  
  849. private int
  850. z1_push(void *callback_data, const fixed * pf, int count)
  851. {
  852.     gs_type1exec_state *pcxs = callback_data;
  853.     i_ctx_t *i_ctx_p = pcxs->i_ctx_p;
  854.     const fixed *p = pf + count - 1;
  855.     int i;
  856.  
  857.     check_ostack(count);
  858.     for (i = 0; i < count; i++, p--) {
  859.     osp++;
  860.     make_real(osp, fixed2float(*p));
  861.     }
  862.     return 0;
  863. }
  864.  
  865. private int
  866. z1_pop(void *callback_data, fixed * pf)
  867. {
  868.     gs_type1exec_state *pcxs = callback_data;
  869.     i_ctx_t *i_ctx_p = pcxs->i_ctx_p;
  870.     double val;
  871.     int code = real_param(osp, &val);
  872.  
  873.     if (code < 0)
  874.     return code;
  875.     *pf = float2fixed(val);
  876.     osp--;
  877.     return 0;
  878. }
  879.  
  880. /* Define the Type 1 procedure vector. */
  881. const gs_type1_data_procs_t z1_data_procs = {
  882.     z1_glyph_data, z1_subr_data, z1_seac_data, z1_push, z1_pop
  883. };
  884.  
  885. /* ------ Font procedures for Type 1 fonts ------ */
  886.  
  887. /*
  888.  * Get a Type 1 or Type 2 glyph outline.  This is the glyph_outline
  889.  * procedure for the font.
  890.  */
  891. int
  892. zchar1_glyph_outline(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
  893.              gx_path *ppath)
  894. {
  895.     gs_font_type1 *const pfont1 = (gs_font_type1 *)font;
  896.     ref gref;
  897.     gs_const_string charstring;
  898.     int code;
  899.  
  900.     glyph_ref(glyph, &gref);
  901.     code = zchar_charstring_data(font, &gref, &charstring);
  902.     if (code < 0)
  903.     return code;
  904.     return zcharstring_outline(pfont1, &gref, &charstring, pmat, ppath);
  905. }
  906. /*
  907.  * Get a glyph outline given a CharString.  The glyph_outline procedure
  908.  * for CIDFontType 0 fonts uses this.
  909.  */
  910. int
  911. zcharstring_outline(gs_font_type1 *pfont1, const ref *pgref,
  912.             const gs_const_string *pgstr,
  913.             const gs_matrix *pmat, gx_path *ppath)
  914. {
  915.     const gs_const_string *pchars = pgstr;
  916.     int code;
  917.     gs_type1exec_state cxs;
  918.     gs_type1_state *const pcis = &cxs.cis;
  919.     static const gs_log2_scale_point no_scale = {0, 0};
  920.     const gs_type1_data *pdata;
  921.     const ref *pfdict;
  922.     ref *pcdevproc;
  923.     int value;
  924.     gs_imager_state gis;
  925.     double sbw[4];
  926.     gs_point mpt;
  927.  
  928.     pdata = &pfont1->data;
  929.     if (pgstr->size <= max(pdata->lenIV, 0))
  930.     return_error(e_invalidfont);
  931.     pfdict = &pfont_data(pfont1)->dict;
  932.     if (dict_find_string(pfdict, "CDevProc", &pcdevproc) > 0)
  933.     return_error(e_rangecheck); /* can't call CDevProc from here */
  934.     switch (pfont1->WMode) {
  935.     default:
  936.     code = zchar_get_metrics2((gs_font_base *)pfont1, pgref, sbw);
  937.     if (code)
  938.         break;
  939.     /* falls through */
  940.     case 0:
  941.     code = zchar_get_metrics((gs_font_base *)pfont1, pgref, sbw);
  942.     }
  943.     if (code < 0)
  944.     return code;
  945.     cxs.present = code;
  946.     /* Initialize just enough of the imager state. */
  947.     if (pmat)
  948.     gs_matrix_fixed_from_matrix(&gis.ctm, pmat);
  949.     else {
  950.     gs_matrix imat;
  951.  
  952.     gs_make_identity(&imat);
  953.     gs_matrix_fixed_from_matrix(&gis.ctm, &imat);
  954.     }
  955.     gis.flatness = 0;
  956.     code = gs_type1_interp_init(&cxs.cis, &gis, ppath, &no_scale, true, 0,
  957.                 pfont1);
  958.     if (code < 0)
  959.     return code;
  960.     cxs.cis.charpath_flag = true;    /* suppress hinting */
  961.     gs_type1_set_callback_data(pcis, &cxs);
  962.     switch (cxs.present) {
  963.     case metricsSideBearingAndWidth:
  964.     mpt.x = sbw[0], mpt.y = sbw[1];
  965.     gs_type1_set_lsb(pcis, &mpt);
  966.     /* falls through */
  967.     case metricsWidthOnly:
  968.     mpt.x = sbw[2], mpt.y = sbw[3];
  969.     gs_type1_set_width(pcis, &mpt);
  970.     case metricsNone:
  971.     ;
  972.     }
  973.     /* Continue interpreting. */
  974. icont:
  975.     code = pfont1->data.interpret(pcis, pchars, &value);
  976.     switch (code) {
  977.     case 0:        /* all done */
  978.     /* falls through */
  979.     default:        /* code < 0, error */
  980.     return code;
  981.     case type1_result_callothersubr:    /* unknown OtherSubr */
  982.     return_error(e_rangecheck); /* can't handle it */
  983.     case type1_result_sbw:    /* [h]sbw, just continue */
  984.     type1_cis_get_metrics(pcis, cxs.sbw);
  985.     pchars = 0;
  986.     goto icont;
  987.     }
  988. }
  989.  
  990. /*
  991.  * Redefine glyph_info to take Metrics[2] and CDevProc into account.
  992.  * (If CDevProc is present, return e_rangecheck, since we can't call the
  993.  * interpreter from here.)
  994.  */
  995. int
  996. z1_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
  997.           int members, gs_glyph_info_t *info)
  998. {
  999.     ref gref;
  1000.     ref *pcdevproc;
  1001.     gs_font_type1 *const pfont = (gs_font_type1 *)font;
  1002.     gs_font_base *const pbfont = (gs_font_base *)font;
  1003.     int wmode = pfont->WMode;
  1004.     const ref *pfdict = &pfont_data(pbfont)->dict;
  1005.     double sbw[4];
  1006.     int width_members = members & (GLYPH_INFO_WIDTH0 << wmode);
  1007.     int default_members = members - width_members;
  1008.     int done_members = 0;
  1009.     int code;
  1010.  
  1011.     if (!width_members)
  1012.     return gs_type1_glyph_info(font, glyph, pmat, members, info);
  1013.     if (dict_find_string(pfdict, "CDevProc", &pcdevproc) > 0)
  1014.     return_error(e_rangecheck); /* can't handle it */
  1015.     glyph_ref(glyph, &gref);
  1016.     if (width_members == GLYPH_INFO_WIDTH1) {
  1017.     code = zchar_get_metrics2(pbfont, &gref, sbw);
  1018.     if (code > 0) {
  1019.         info->width[1].x = sbw[2];
  1020.         info->width[1].y = sbw[3];
  1021.         done_members = width_members;
  1022.         width_members = 0;
  1023.     }
  1024.     }
  1025.     if (width_members) {
  1026.     code = zchar_get_metrics(pbfont, &gref, sbw);
  1027.     if (code > 0) {
  1028.         info->width[wmode].x = sbw[2];
  1029.         info->width[wmode].y = sbw[3];
  1030.         done_members = width_members;
  1031.         width_members = 0;
  1032.     }
  1033.     }
  1034.     default_members |= width_members;
  1035.     if (default_members) {
  1036.     code = gs_type1_glyph_info(font, glyph, pmat, default_members, info);
  1037.  
  1038.     if (code < 0)
  1039.         return code;
  1040.     } else
  1041.     info->members = 0;
  1042.     info->members |= done_members;
  1043.     return 0;
  1044. }
  1045.